var FunctionManager = function(){
	var self = this;
	self.funcs = [];
	
	self.register = function(name, description, argsArray){
		builder.clear();
		builder.build("{0}(", name);
		var count = argsArray.length;
		if(count > 0){
			builder.build(argsArray[0]);
		}
		for(var i = 1; i < count; i+=1){
			builder.build(",{0}", argsArray[i]);
		}
		builder.build(")");
		var signature = builder.toString();
		builder.clear();
		
		builder.buildLine(signature);
		builder.buildLine(description);
		var text = builder.toString();
		builder.clear();
		
		self.funcs.push({
			"name":name,
			"description":description,
			"argsArray":argsArray,
			"signature":signature,
			"text":text
		});
	};
	
	self.buildUI = function(parentId, pasteTextAreaId){
		self.funcs.sort(function(a,b){return (a.signature > b.signature ? 1 : -1)});
	
		var parent = document.getElementById(parentId);
		var pasteTextArea = document.getElementById(pasteTextAreaId);
		
		var textarea = document.createElement("TEXTAREA");
		
		var select = document.createElement("SELECT");
		var count = self.funcs.length;
		for(var i = 0; i < count; i++){
			var option = document.createElement("OPTION");
			option.innerHTML = self.funcs[i].signature;
			select.appendChild(option);
		}
		
		select.setAttribute("multiple","multiple");
		
		select.onchange = function(e){
			var index = select.selectedIndex;
			textarea.value = self.funcs[index].text;
		};
		
		var button = document.createElement("BUTTON");
		button.innerHTML="Copy signature!";
		button.onclick=function(e){
			var index = select.selectedIndex;
			if(index > -1){
				pasteTextArea.value += self.funcs[index].signature;
			}
		};
		var header = document.createElement("DIV");
		header.innerHTML = "JavaScript functions:";
		
		var description = document.createElement("DIV");
		description.innerHTML = "Function description:";
		
		parent.appendChild(header);
		parent.appendChild(select);
		parent.appendChild(button);
		parent.appendChild(description);
		parent.appendChild(textarea);
	};
};

var functionManager = new FunctionManager();
